package gsb.gsb;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import modele.DAO;
import modele.Medecin;

public class AutreActivity extends ListActivity {
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        //on va chercher la vue
        setContentView(R.layout.autrevue);
        TextView label = (TextView)findViewById(R.id.message);
        Intent inter = getIntent();
        String dep = inter.getStringExtra("LeDep");
        label.setText(dep);
        final List<Medecin> lesMeds = DAO.getLesMeds(dep);
        MedAdapter adapter = new MedAdapter(lesMeds, this);
        setListAdapter(adapter);
        
        Button filtrer = (Button) findViewById(R.id.button);
        filtrer.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                EditText message = (EditText) findViewById(R.id.message);
                List<Medecin> lesMedsList = new ArrayList<Medecin>();
                for (Medecin unMed : lesMeds){
                    if (unMed.getNom().toUpperCase().startsWith(message.getText().toString().toUpperCase())){
                        lesMedsList.add(unMed);
                    }
                }
                MedAdapter adapter = new MedAdapter(lesMedsList, getApplicationContext());
                setListAdapter(adapter);
            }
        });
    }
    
    public void onlistItemClick(final ListView l, View v, final int position, long id){
        Medecin med = (Medecin) l.getItemAtPosition(position);
        final String nTel = med.getTel();
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:" + nTel));
        startActivity(callIntent);
    }
}